﻿using System.Data;
using System.Windows.Forms;
using zyconfig;
using zymarc;

namespace zyui
{
    public partial class MarcGridS : UserControl
    {
        private string _key;
        private string _kjm;
        private string _MarcText;
        private string _Nmarc; //当marc更新
        private int _MarcType;
        public MarcGridS()
        {
            InitializeComponent();
        }
        public void GetGridView()
        {
            this.dataGridView1.Update();
            this.dataGridView1.Refresh();
            string str = string.Empty;
            string[] strArray = MarcText.Split(new char[] { '\x001e' });
            for (int item = 0; item < dataGridView1.Rows.Count; item++)
            {
                string name = "";
                if (dataGridView1.Rows[item].Cells[1].Value != null)
                    name = dataGridView1.Rows[item].Cells[1].Value.ToString();
                string qyt = "";
                if (dataGridView1.Rows[item].Cells[2].Value != null)
                    qyt = dataGridView1.Rows[item].Cells[2].Value.ToString();
                string nr = "";
                if (dataGridView1.Rows[item].Cells[3].Value != null)
                    nr = dataGridView1.Rows[item].Cells[3].Value.ToString();
                string oldValue = '\x001f'.ToString();
                string newValue = '@'.ToString();
                nr = nr.Replace('|', '@').Replace(newValue, oldValue);
                str += '\x001e' + name + qyt + nr;
            }
            this._Nmarc = strArray[0].ToString() + str + '\x001e';
        }
        public void SetGridView()
        {
            int num;
            DataTable table = new DataTable("marc");
            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "字段名称";
            column.AutoIncrement = false;
            column.Caption = "字段名称";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "字段";
            column.AutoIncrement = false;
            column.Caption = "字段";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "指";
            column.AutoIncrement = false;
            column.Caption = "指";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "字段内容";
            column.AutoIncrement = false;
            column.Caption = "字段内容";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);
            string str = this._MarcText;
            string oldValue = '\x001f'.ToString();
            string newValue = '@'.ToString();
            string[] strArray = str.Replace(oldValue, newValue).Replace('@', '|').Split(new char[] { '\x001e' });
            //string[] strArray = str.Replace(oldValue, newValue).Split(new char[] { '\x001e' });
            for (num = 1; num < strArray.Length; num++)
            {
                string str4 = strArray[num].ToString();
                if (str4.Length >= 3)
                {
                    DataRow row;
                    string strNode = str4.Substring(0, 3);
                    string strT = "";
                    using (DbOperHandler _open = new Common().Doh())
                    {
                        _open.Reset();
                        _open.ConditionExpress = " MARC类型=" + this._MarcType + " and 标识符='" + strNode + "'";
                        strT = (string)_open.GetField("MARC数据字典", "名称缩写");
                    }
                    if (((strNode == "001") || (strNode == "005")) || (strNode == "008"))
                    {
                        row = table.NewRow();
                        row["字段名称"] = strT;
                        row["字段"] = strNode;
                        row["指"] = "";
                        row["字段内容"] = str4.Substring(3, str4.Length - 3);
                        table.Rows.Add(row);
                    }
                    else
                    {
                        row = table.NewRow();
                        row["字段名称"] = strT;
                        row["字段"] = strNode;
                        row["指"] = str4.Substring(3, 2);
                        row["字段内容"] = str4.Substring(5, str4.Length - 5);
                        table.Rows.Add(row);
                    }
                }
            }
            this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            this.dataGridView1.Rows.Clear();
            for (num = 0; num <= (table.Rows.Count - 1); num++)
            {
                int num2 = this.dataGridView1.Rows.Add();
                DataGridViewRow row2 = this.dataGridView1.Rows[num2];
                row2.Cells["字段名称"].Value = table.Rows[num]["字段名称"].ToString();
                row2.Cells["字段标识"].Value = table.Rows[num]["字段"].ToString();
                row2.Cells["指示符"].Value = table.Rows[num]["指"].ToString();
                //StringBuilder html = new StringBuilder();
                string nr = table.Rows[num]["字段内容"].ToString();
                //nr = nr.Replace("|", "<font color='red'>|</font>");
                //html.Append(nr);
                row2.Cells["内容"].Value = nr;
            }
        }

        public void setPy()
        {
            MarcConvert n = new MarcConvert();
            n.MarcText = this._MarcText;
            string[] zds = new string[] { "200", "701", "702", "711", "712" };
            foreach (string szd in zds)
            {
                int m = n.GetFieldCount(szd);
                for (int i = 1; i <= m; i++)
                {
                    string nt = n.GetASubField(szd, "a", i);
                    string str = string.Empty;
                    if (!string.IsNullOrEmpty(nt))
                    {
                        string pynr = n.GetASubField(szd, "9", i);
                        if (string.IsNullOrEmpty(pynr))
                        {
                            var rlt = StringAna.getString(nt);
                            if (rlt != null)
                            {
                                foreach (var item in rlt)
                                {
                                    str += StringAna.getPin1(item, nt);
                                }
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        //str = "|9" + str.Substring(0, str.Length - 1);
                        str = str.Substring(0, str.Length - 1);
                        n.ReplaceSubfield(szd, "9", i, str);
                        /**
                        int index = Nmarc.IndexOf(nt);
                        StringBuilder sb = new StringBuilder(Nmarc);
                        sb.Insert(index + nt.Length, str);//插入
                        this._MarcText = sb.ToString();
                        **/
                        this._MarcText = n.MarcText;
                    }
                }
            }
        }
        public string key
        {
            get
            {
                return this._key;
            }
            set
            {
                this._key = value;
            }
        }
        public string MarcText
        {
            get
            {
                return this._MarcText;
            }
            set
            {
                this._MarcText = value;
            }
        }
        public string Nmarc
        {
            get
            {
                return this._Nmarc;
            }
            set
            {
                this._Nmarc = value;
            }
        }
        public int MarcType
        {
            get
            {
                return this._MarcType;
            }
            set
            {
                this._MarcType = value;
            }
        }
        public string kjm
        {
            get
            {
                return this._kjm;
            }
            set
            {
                this._kjm = value;
            }
        }
    }
}
